// 登录参考文献：https://blog.csdn.net/qq_22078107/article/details/104899787?spm=1001.2014.3001.5506

import Vue from 'vue'
import Vuex from 'vuex'
import imModule from './modules/im'; // 引入im模块
import notificationService from '@/utils/notificationService.js';
// 引入统一配置文件
import config from '@/config/index.js';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: { //全局变量定义处
		hasLogin: false, //用户是否登录
		cityInfo: {
			code: null,
			name: "未知"
		}, //用于存放城市信息
		userInfo: uni.getStorageSync('userInfo'), //用于存放用户账号
		phoneNumber: uni.getStorageSync('phoneNumber'),
		friendList: uni.getStorageSync('friendList'),
		timeAxisList: uni.getStorageSync('timeAxis'),
		refreshOwnPost: false, //是否刷新时光轴
		phoneContacs: [], //手机联系人
		ossBaseUrl: config.cdnURL || 'http://www.hacn86.cn', // 确保这里是你的CDN域名
		qiNiuUploadUrl: 'https://up.qiniup.com', // 确保这里是你的文件上传地址
		chatHistoryCache: {}, // 用于存储聊天记录缓存, 格式为 { 'userId': [messages] }
		// 系统与导航栏信息
		systemInfo: {
			statusBarHeight: 0, // 状态栏高度
			navBarHeight: 44, // 导航栏内容区高度 (不含状态栏)
			capsuleInfo: {}, // 胶囊按钮信息对象
			navBarTotalHeight: 0, // 自定义导航栏总高度 (状态栏 + 导航栏)
			capsuleRightPadding: 0 // 为右侧内容预留的边距
		},
	},
	mutations: { //全局方法定义处
		login(state, provider) {
			state.hasLogin = true;
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');
			if (notificationService.isConnected()) {
				notificationService.disconnect();
			}
		},
		setPhoneNumber(state, provider) {
			state.phoneNumber = provider
			uni.setStorage({
				key: 'phoneNumber',
				data: provider
			})
		},
		setUserInfo(state, provider) {
			state.userInfo = provider
			uni.setStorage({
				key: 'userInfo',
				data: provider
			})
		},
		setTimeAxisList(state, provider) {
			state.timeAxisList = provider
			uni.setStorage({
				key: 'timeAxisList',
				data: provider
			})
		},
		setFriendList(state, provider) {
			state.friendList = provider
			uni.setStorage({
				key: 'friendList',
				data: provider
			})
		},
		setCityInfo(state, provider) {
			state.cityInfo = provider
			uni.setStorage({
				key: 'cityInfo',
				data: provider
			})
		},
		setRefreshOwnPost(state, provider) {
			state.refreshOwnPost = provider
		},
		setOssBaseUrl(state, provider) {
			state.ossBaseUrl = provider
			uni.setStorage({
				key: 'ossBaseUrl',
				data: provider
			})
		},
		setPhoneContacs(state, provider) {
			state.phoneContacs = provider
			uni.setStorage({
				key: 'phoneContacs',
				data: provider
			})
		},
		// 一个专门的 mutation 来一次性更新所有系统信息
		SET_SYSTEM_INFO(state, payload) {
			state.systemInfo = {
				...state.systemInfo,
				...payload
			};
		},
		// 从本地存储加载聊天记录缓存到 Vuex state
		LOAD_CHAT_HISTORY_FROM_STORAGE(state) {
			const cache = uni.getStorageSync('chatHistoryCache');
			if (cache) {
				state.chatHistoryCache = cache;
			}
		},

		// 保存指定用户的聊天记录到 Vuex 和本地存储
		SAVE_CHAT_HISTORY(state, {
			userId,
			history
		}) {
			if (!userId || !history) return;
			// 使用 Vue.set 确保响应式更新
			Vue.set(state.chatHistoryCache, userId, history);
			uni.setStorageSync('chatHistoryCache', state.chatHistoryCache);
		},

		// 清空所有聊天记录缓存 (可选)
		CLEAR_ALL_CHAT_HISTORY(state) {
			state.chatHistoryCache = {};
			uni.removeStorageSync('chatHistoryCache');
		}
	},
	getters: {
		// 根据 userId 获取聊天记录
		getChatHistoryByUserId: (state) => (userId) => {
			return state.chatHistoryCache[userId] || null;
		},
		// 创建一个 getter 方便获取导航栏总高度
		navBarTotalHeight: state => {
			return state.systemInfo.statusBarHeight + state.systemInfo.navBarHeight;
		},
	},
	modules: {
		im: imModule, // 注册im模块
	}
})
export default store